/*
 *  Copyright (C) 2010 Jonathan MERCIER <bioinfornatics@fedoraproject.org>,
 *  Louise-Amélie SCHMITT <louise-amelie.schmitt@etu.u-bordeaux1.fr>,
 *  Florence Maurier <florence_maurier@orange.fr>,
 *  Marianne Dussart <marianne.dussart@orange.fr>,
 *  Nicolas Fontaine <nifontaine@gmail.com>
 *
 *  JACoMode is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  JACoMode is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with JACoMode.  If not, see <http://www.gnu.org/licenses/>.
 */


function loadinfo(data)
{
    dwr.util.setValue('center', data, {escapeHtml:false});
}

function forward(webPage)
{
    // alert(webPage);
    Content.getInclude(webPage,loadinfo);
    //dwr.engine.setActiveReverseAjax(true);
}

function changeImage(image,elementName)
{
   document.images[elementName].src=image;
   return true;
}

function overImage(elementName)
{
   document.images[elementName].src='IMAGES/buttons/'+elementName+"2.png";
   return true;
}
function outImage(elementName)
{
    document.images[elementName].src='IMAGES/buttons/'+elementName+"1.png";
    return true;
}

function overInput(elementName)
{
    if (document.getElementById(elementName).value == elementName)
    {
        document.getElementById(elementName).value='';
    }
   //document.forms['acomResults'].elements[elementName];
   return true;
}

function checkInputMail(elementName)
{
    var mail    = document.getElementById(elementName).value;
    var place   = mail.indexOf("@",1);
    var point   = mail.indexOf(".",place+1);
    var isMail  = false;

    if ((place > -1)&&(mail.length >2)&&(point > 1))
    {
		isMail = true
    }
	else
    {
		alert('Please enter a valid e-mail address.');
        cleanInput(elementName);
		isMail = false;
    }
   return isMail;
}
function checkInputNumber(elementName,nombre)
{
    if (isNaN(nombre))
    {
        alert('Please enter a valid number.');
        cleanInput(elementName);
    }
    else
    {
        var size    = nombre.length;
        var pos     = 0;
        var charact = "";
        var isInt   = true;
        while (isInt && pos<size)
        {
            charact = nombre.charAt(pos);
            if ('0'<=charact && charact<='9')
            {
                pos++;
            }
            else
            {
                isInt = false;
                alert('Please enter a valid integer.');
                cleanInput(elementName);
            }
        }
    }
   return true;
}

function cleanInput(elementName)
{
    document.getElementById(elementName).value=elementName;
}


function outInput(elementName,type)
{
    var node = document.getElementById(elementName)
    
    if (document.getElementById(elementName).value=='')
    {
        node.value= elementName;
    }
    else
    {
        if (type == "mail")
        {
            checkInputMail(elementName);
        }
        else if (type == "number")
        {
            var nombre1;
            var nombre2;
            var numString = String( node.value );

            var position = numString.lastIndexOf('-');
            if (position == -1)
            {
                checkInputNumber(elementName, numString);
            }
            else
            {
                nombre1 = numString.substr(0, position);
                nombre2 = numString.substr(position+1);
                checkInputNumber(elementName, nombre1);
                checkInputNumber(elementName, nombre2);
            }
        }
    }
    
            
   
   return true;
}

function addField(area,field,limit, label)
{
    if(!document.getElementById) return; //Prevent older browsers from getting any further.
    var field_area = document.getElementById(area);
    var all_inputs = field_area.getElementsByTagName("input"); //Get all the input fields in the given area.
    //Find the count of the last element of the list. It will be in the format '<field><number>'. If the
    //              field given in the argument is 'friend_' the last id will be 'friend_4'.
    var last_item = all_inputs.length - 1;
    var last = all_inputs[last_item].id;
    var count = Number(last.split("_")[1]) + 1;

    //If the maximum number of elements have been reached, exit the function.
    //If the given limit is lower than 0, infinite number of fields can be created.
    if(count > limit && limit > 0) return;
    var li;var input1;var input2;var input3;
    if(Prototype.Browser.WebKit)//W3C Dom method
    {
        li                  = document.createElement("li");
        input1              = document.createElement("input");
        input2              = document.createElement("input");
        input3              = document.createElement("input");

        input1.type         = "file"; //Type of field - can be any valid input type like text,file,checkbox etc.
        input1.name         = field+count;
        input1.id           = field+count;
        input1.className    = "inputTypeFile"
        input1.value        = "";

        input2.type         = "button";
        input2.value        = "Select a .csv file";
        input2.onclick      = "getfile('"+(field+count)+"')";
        input2.className    = "inputTypeButton";
       
        input3.type         = "text";
        input3.id           = "selectedfile"+(field+count);
        input3.size         = "60";

        li.appendChild(input1);
        li.appendChild(input2);
        li.appendChild(input3);
        field_area.appendChild(li);
    }
    else if(Prototype.Browser.Gecko)
    {
        li                  = document.createElement("li");
        input1              = document.createElement("input");
       
        input1.type         = "file"; //Type of field - can be any valid input type like text,file,checkbox etc.
        input1.name         = field+count;
        input1.id           = field+count;
        input1.value        = "";

        li.appendChild(document.createTextNode(label));
        li.appendChild(input1);
        field_area.appendChild(li);
    }
    else//Older Method
    {
        field_area.innerHTML += "<li><input type=\"file\" name='"+(field+count)+"' id='"+(field+count)+"class=\"inputTypeFile\" value=\"\"/><input type=\"button\" value=\"Select a .csv file\" onclick=\"getfile('"+(field+count)+"') class=\"inputTypeButton\"/><input type=\"text\" id=\"selectedfile"+(field+count)+" size=\"60\" />";
    }
}

function getfile(name)
{
    var inputTypeFile = document.getElementById(name);
    inputTypeFile.click();
    document.getElementById('selectedfile'+name).value=inputTypeFile.value
}

function submit_out(input_out)
{
    var httpRequest = null;
    if(window.XMLHttpRequest)
    { // Mozilla, Safari, Firefox ...
        httpRequest = new XMLHttpRequest();
        if (httpRequest.overrideMimeType)
        {
            httpRequest.overrideMimeType('text/xml');
        }
    }
    else if (window.ActiveXObject)
    { // IE
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }
    httpRequest.open('POST', forward('bilanResult'), true);
    httpRequest.setRequestHeader('Content-type','overallReaction/result');
    httpRequest.send("input_out="+input_out);
}

function checkIt(string)
{
        place       = detect.indexOf(string) + 1;
        thestring   = string;
        return place;
}

function showEfm(index,efmOccurence,efmlocation)
{
    if (index ==0)
    {
        document.getElementById('occurence').innerHTML  = "  ---  ";
        document.getElementById('location').innerHTML   = "  ---  ";
    }
    else
    {
        document.getElementById('occurence').innerHTML  = efmOccurence[index-1];
        document.getElementById('location').innerHTML   = efmlocation[index-1];
    }
}

function showClass(index,nbClassByMotifSize,iDClassByMotifSize)
{
    if (index ==0)
    {
        document.getElementById('nbClass2').innerHTML  = "  ---  ";
        document.getElementById('listClass2').innerHTML  = "  ---  ";
    }
    else
    {
        document.getElementById('nbClass2').innerHTML   = nbClassByMotifSize[index-1];
        document.getElementById('listClass2').innerHTML = iDClassByMotifSize[index-1];
    }
}

function isFocus(elementName)
{
    var result;

    try //IE
    {
        result = !!(elementName.hasFocus||elementName==document.activeElement);
        if (result) { return result; }
    }
    catch (ex) {}
    try // FF; Safari
    {
        var sel = false;
        sel     = this.window.getSelection().getRangeAt(0);
        if (!sel.collapsed) { return false; }
        var sel2= document.createRange();
        sel2.setStartBefore(elementName);
        sel2.setEndBefore(elementName);
        result = true;
        result = result && sel.startOffset == sel2.startOffset
        result = result && sel.endOffset == sel2.endOffset
        return result;
    }
    catch (ex) {}
    try // Opera
    {
        return !!(elementName.selectionStart||elementName.selectionEnd);
    }
    catch (ex) {}
    return false;
}